package net.xo68.destiny.manager.mapper;


import net.xo68.destiny.manager.entity.NodeHeartbeat;
import org.apache.ibatis.annotations.*;
import java.util.List;

/** 
 * NodeHeartbeat mybatis mapper
 * @author wuxie(16349023@qq.com)
 * @date 2020年11月06日 22:25:25
 *
 */
public interface NodeHeartbeatMapper {


	
	@Select("SELECT * FROM job_node_heartbeat WHERE node_host = #{nodeHost}")
    @Results(value = { 
    @Result(id = true, column = "node_host", property = "nodeHost"),
        @Result(column = "node_type_name", property = "nodeTypeName"),
        @Result(column = "work_group_name", property = "workGroupName"),
        @Result(column = "cpu_usage", property = "cpuUsage"),
        @Result(column = "memory_usage", property = "memoryUsage"),
        @Result(column = "load_average", property = "loadAverage"),
        @Result(column = "last_time", property = "lastTime")
    })
    NodeHeartbeat find(@Param("nodeHost") String nodeHost);
	
	@Select("SELECT * FROM job_node_heartbeat")
    @Results(value = { 
    @Result(id = true, column = "node_host", property = "nodeHost"),
        @Result(column = "node_type_name", property = "nodeTypeName"),
        @Result(column = "work_group_name", property = "workGroupName"),
        @Result(column = "cpu_usage", property = "cpuUsage"),
        @Result(column = "memory_usage", property = "memoryUsage"),
        @Result(column = "load_average", property = "loadAverage"),
        @Result(column = "last_time", property = "lastTime")
    })
    List<NodeHeartbeat> list();

    @Select("SELECT * FROM job_node_heartbeat where last_time >= DATE_ADD(NOW(),INTERVAL -#{timeoutSecond} SECOND)")
    @Results(value = {
            @Result(id = true, column = "node_host", property = "nodeHost"),
            @Result(column = "node_type_name", property = "nodeTypeName"),
            @Result(column = "work_group_name", property = "workGroupName"),
            @Result(column = "cpu_usage", property = "cpuUsage"),
            @Result(column = "memory_usage", property = "memoryUsage"),
            @Result(column = "load_average", property = "loadAverage"),
            @Result(column = "last_time", property = "lastTime")
    })
    List<NodeHeartbeat> validList(long timeoutSecond);

    /**
     * 移除过期
     * @param timeoutSecond
     * @return
     */
    @Delete("delete FROM job_node_heartbeat where last_time < DATE_ADD(NOW(),INTERVAL -#{timeoutSecond} SECOND)")
    int removeExpired(@Param("timeoutSecond") long timeoutSecond);
	
	@Insert("insert into job_node_heartbeat (node_host ,node_type_name ,work_group_name ,cpu_usage ,memory_usage ,load_average ,last_time ) values (#{nodeHost} , #{nodeTypeName} , #{workGroupName} , #{cpuUsage} , #{memoryUsage} , #{loadAverage} , #{lastTime}  )")
    @Options(useGeneratedKeys = true, keyProperty = "nodeHost")
    int insert(NodeHeartbeat nodeHeartbeat);
	
	@Update({"update job_node_heartbeat set",
      " node_type_name=#{nodeTypeName}, work_group_name=#{workGroupName}, cpu_usage=#{cpuUsage}, memory_usage=#{memoryUsage}, load_average=#{loadAverage}, last_time=#{lastTime} ",
      "where node_host = #{nodeHost}"
    })
    int update(NodeHeartbeat nodeHeartbeat);

	@Delete("delete FROM job_node_heartbeat WHERE node_host = #{nodeHost} and node_type_name= #{nodeTypeName}")
	int delete(@Param("nodeHost") String nodeHost, @Param("nodeTypeName") String nodeTypeName);
}